package com.wq.httpclientcore.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wq.httpclientcore.entities.po.ApiConfig;
import com.wq.httpclientcore.entities.po.AuthConfig;
import com.wq.httpclientcore.entities.po.DbQueryConfig;
import com.wq.httpclientcore.entities.req.query.AuthConfigREQ;
import com.wq.httpclientcore.event.AuthConfigDeletedEvent;
import com.wq.httpclientcore.mapper.AuthConfigMapper;
import com.wq.httpclientcore.service.ApiConfigService;
import com.wq.httpclientcore.service.AuthConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.httpclientcore.utils.QueryWrapperBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 鉴权配置 服务实现类
 *
 * @author wangqi
 * @since 2025-03-13 10:46:59
 */
@Service
public class AuthConfigServiceImpl extends ServiceImpl<AuthConfigMapper, AuthConfig> implements AuthConfigService {

    @Autowired
    private ApplicationEventPublisher eventPublisher;
    @Override
    public List<AuthConfig> list(AuthConfigREQ req) {
        req.setOrderByField("updated_at");
        LambdaQueryWrapper<AuthConfig> queryWrapper = QueryWrapperBuilder.queryWrapper(AuthConfig.class, req);
        return list(queryWrapper);
    }

    @Override
    public Page<AuthConfig> pageList(AuthConfigREQ req) {
        req.setOrderByField("updated_at");
        LambdaQueryWrapper<AuthConfig> queryWrapper = QueryWrapperBuilder.queryWrapper(AuthConfig.class, req);
        Page<AuthConfig> page = Page.of(req.getPageNo(), req.getPageSize());
        return page(page, queryWrapper);
    }

    @Override
    public Map<Long, AuthConfig> id2PO(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }

        List<AuthConfig> list = this.listByIds(ids);
        return list.stream().collect(Collectors.toMap(AuthConfig::getId, e -> e));
    }

    @Override
    public Boolean saveAuthConfig(AuthConfig po) {
        int result = baseMapper.insertAuthConfig(po);
        return result > 0;
    }

    @Override
    public Boolean updateAuthConfig(AuthConfig po) {
        return baseMapper.updateAuthConfigById(po);
    }

    @Transactional
    @Override
    public Boolean deletedById(List<Long> list) {
        try {
            boolean deletedAuthFlag = removeByIds(list);
            if (deletedAuthFlag) {
                // 发布事件，携带被删除的 AuthConfig ID 列表
                eventPublisher.publishEvent(new AuthConfigDeletedEvent(this, list));
            }
            return deletedAuthFlag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
